package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.GroupMchntInfoMapper;
import cc.rengu.igas.bomp.common.entity.GroupMchntInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.StringUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 集团商户信息正式表接口实现类
 * Created by 赵奎伍 on 2020/3/24
 */
public class GroupMchntInfoMapperImpl implements GroupMchntInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);


    @Override
    public List<GroupMchntInfo> selectGroupMchntInfoByInstId(String instId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompGroupMchntInfoByInstId");
        GroupMchntInfo groupMchntInfo = new GroupMchntInfo();
        groupMchntInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, groupMchntInfo, GroupMchntInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("获取集团商户信息失败,instId:<{}>", instId);
            return null;
        }
        return objectList.stream().map(item -> (GroupMchntInfo) item).collect(Collectors.toList());
    }


    @Override
    public List<GroupMchntInfo> selectGroupMchntInfoByGroupMchntNoOrGroupMchntNameOrSignInstId(String instId, String groupMchntNo, String groupMchntName, String signInstId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompGroupMchntInfoByGroupMchntNoOrGroupMchntNameOrSignInstId");
        GroupMchntInfo groupMchntInfo = new GroupMchntInfo();
        groupMchntInfo.setInstId(instId);
        if (null != groupMchntNo && !groupMchntNo.isEmpty()) {
            groupMchntInfo.setGroupMchntNo(groupMchntNo);
        }
        if (null != groupMchntName && !groupMchntName.isEmpty()) {
            groupMchntInfo.setGroupMchntName(groupMchntName);
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            groupMchntInfo.setManageInstId(signInstId);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, groupMchntInfo, GroupMchntInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据集团商户号查询或集团商户名称模糊查询或根据签约机构代码查询失败,instId:<{}>,groupMchntNo:<{}>,groupMchntName:<{}>,signInstId:<{}>", instId, groupMchntNo, groupMchntName, signInstId);
            return null;
        }
        return objectList.stream().map(item -> (GroupMchntInfo) item).collect(Collectors.toList());
    }


    @Override
    public PageResult selectGroupMchntInfoPageByInstId(String instId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompGroupMchntInfoByInstId");
        GroupMchntInfo groupMchntInfo = new GroupMchntInfo();
        groupMchntInfo.setInstId(instId);

        return dbsUtil.dbsFetchPageListData(sqlStr, groupMchntInfo, GroupMchntInfo.class, pageInfo);
    }


    @Override
    public PageResult selectGroupMchntInfoByGroupMchntNoOrGroupMchntNameOrSignInstId(String instId, String groupMchntNo, String groupMchntName, String signInstId, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBompGroupMchntInfoByGroupMchntNoOrGroupMchntNameOrSignInstId");
        GroupMchntInfo groupMchntInfo = new GroupMchntInfo();
        groupMchntInfo.setInstId(instId);
        if (null != groupMchntNo && !groupMchntNo.isEmpty()) {
            groupMchntInfo.setGroupMchntNo(groupMchntNo);
        }
        if (null != groupMchntName && !groupMchntName.isEmpty()) {
            groupMchntInfo.setGroupMchntName(groupMchntName);
        }
        if (null != signInstId && !signInstId.isEmpty()) {
            groupMchntInfo.setManageInstId(signInstId);
        }

        return dbsUtil.dbsFetchPageListData(sqlStr, groupMchntInfo, GroupMchntInfo.class, pageInfo);
    }

    @Override
    public List<GroupMchntInfo> selectGroupMchntInfoByManageInstIds(String instId, String groupMchntNo, String groupMchntName, String signInstId, String manageInstIds) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectGroupMchntInfoByManageInstIds");
        GroupMchntInfo groupMchntInfo = new GroupMchntInfo();
        groupMchntInfo.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(manageInstIds)) {
            groupMchntInfo.setRemark1(manageInstIds);
        }
        if (!StringUtil.isEmptyOrNull(groupMchntNo)) {
            groupMchntInfo.setGroupMchntNo(groupMchntNo);
        }
        if (!StringUtil.isEmptyOrNull(groupMchntName)) {
            groupMchntInfo.setGroupMchntName(groupMchntName);
        }
        if (!StringUtil.isEmptyOrNull(signInstId)) {
            groupMchntInfo.setManageInstId(signInstId);
        }
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, groupMchntInfo, GroupMchntInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据签约机构代码查询失败,instId:<{}>,manageInstIds:<{}>", instId, manageInstIds);
            return null;
        }
        List<GroupMchntInfo> groupMchntInfoList = objectList.stream().map(item -> (GroupMchntInfo) item).collect(Collectors.toList());
        if (!StringUtil.isEmptyOrNull(manageInstIds)) {
            List<String> manageInstIdList = Arrays.asList(manageInstIds.split(","));
            groupMchntInfoList = groupMchntInfoList.stream().filter(item -> manageInstIdList.contains(item.getManageInstId())).collect(Collectors.toList());
        }
        return groupMchntInfoList;
    }

    @Override
    public PageResult selectGroupMchntInfoByPageAndManageInstIds(String instId, String groupMchntNo, String groupMchntName, String signInstId, String manageInstIds, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectGroupMchntInfoByManageInstIds");
        GroupMchntInfo groupMchntInfo = new GroupMchntInfo();
        groupMchntInfo.setInstId(instId);
        if (!StringUtil.isEmptyOrNull(manageInstIds)) {
            groupMchntInfo.setRemark1(manageInstIds);
        }
        if (!StringUtil.isEmptyOrNull(groupMchntNo)) {
            groupMchntInfo.setGroupMchntNo(groupMchntNo);
        }
        if (!StringUtil.isEmptyOrNull(groupMchntName)) {
            groupMchntInfo.setGroupMchntName(groupMchntName);
        }
        if (!StringUtil.isEmptyOrNull(signInstId)) {
            groupMchntInfo.setManageInstId(signInstId);
        }
        return dbsUtil.dbsFetchPageListData(sqlStr, groupMchntInfo, GroupMchntInfo.class, pageInfo);
    }


    @Override
    public GroupMchntInfo selectGroupMchntInfoByPrimaryKey(String instId, String groupMchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectGroupMchntInfoByPrimaryKey");
        GroupMchntInfo groupMchntInfo = new GroupMchntInfo();
        groupMchntInfo.setInstId(instId);
        groupMchntInfo.setGroupMchntNo(groupMchntNo);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, groupMchntInfo, groupMchntInfo);
        if (0 != iReturnCode) {
            rglog.error("获取集团商户信息失败,groupMchntNo:<{}>,iReturnCode:<{}>", groupMchntNo, iReturnCode);
            return null;
        }
        return groupMchntInfo;
    }
}
